import {
    createRouter,
    createWebHistory
} from 'vue-router'

const router = createRouter({
    history: createWebHistory(),
    routes: [{
            path: '/login',
            component: () => import('@/views/Login.vue')
        },
        {
            path: '/register',
            component: () => import('@/views/Register.vue')
        },
        {
            path: '/reset',
            component: () => import('@/views/Reset.vue')
        },
        {
            path: '/main',
            name: "主窗口",
            redirect: "/chat",
            component: () => import('@/views/Main.vue'),
            children: [{
                    path: "/chat",
                    name: "会话",
                    component: () => import('@/views/chat/Chat.vue'),
                }, {
                    path: "/contact",
                    name: "联系人",
                    redirect: "/contact/searchFriend",
                    component: () => import('@/views/contact/Contact.vue'),
                    children: [{
                            path: "/contact/searchFriend",
                            name: "搜索好友",
                            component: () => import('@/views/contact/SearchFriend.vue'),
                        },
                        {
                            path: "/contact/searchGroup",
                            name: "搜索群组",
                            component: () => import('@/views/contact/SearchGroup.vue'),
                        },
                        {
                            path: "/contact/newFriend",
                            name: "新的朋友",
                            component: () => import('@/views/contact/NewFriend.vue'),
                        },
                        {
                            path: "/contact/createGroup",
                            name: "新建群组",
                            component: () => import('@/views/contact/CreateGroup.vue'),
                        },
                        {
                            path: "/contact/myGroup",
                            name: "我的群组",
                            component: () => import('@/views/contact/MyGroup.vue'),
                        },
                        {
                            path: "/contact/joinGroup",
                            name: "加入群组",
                            component: () => import('@/views/contact/JoinGroup.vue'),
                        },
                        {
                            path: "/contact/myFriend",
                            name: "我的好友",
                            component: () => import('@/views/contact/MyFriend.vue'),
                        }
                    ]
                },
                {
                    path: "/setting",
                    redirect: "/setting/userInfo",
                    name: "设置",
                    component: () => import('@/views/setting/Setting.vue'),
                    children: [{
                        path: "/setting/userInfo",
                        name: "个人信息",
                        component: () => import('@/views/setting/UserInfo.vue'),
                    }]
                }
            ]
        },
    ]
})


const whiteList = ['/login', '/register', '/reset']
router.beforeEach((to, form, next) => {
    const token = localStorage.getItem('token')
    if (token) {
        if (to.path == '/login' || to.path == '/register' || to.path == '/reset' || to.path == '/' || to.path == '') {
            next('/main')
        } else {
            next()
        }
    } else {
        if (whiteList.indexOf(to.path) > -1) {
            next()
        } else {
            next('/login')
        }
    }
})

export default router
